//
//  CYInvestRecordTopCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/23.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYInvestRecordTopCell.h"



@implementation CYInvestRecordTopCellObject

@end



@interface CYInvestRecordTopCell ()

@property(nonatomic, strong)UIImageView *topImageView;
@property(nonatomic, strong)UILabel *statusLabel;
@property(nonatomic, strong)UILabel *dueDayLabel;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIImageView *stateImageView;

@property(nonatomic, strong)UILabel *amountDescLabel;
@property(nonatomic, strong)UILabel *amountLabel;
@property(nonatomic, strong)UILabel *rateDescLabel;
@property(nonatomic, strong)UILabel *rateLabel;
@property(nonatomic, strong)UILabel *profitsDescLabel;
@property(nonatomic, strong)UILabel *profitsLabel;
@property(nonatomic, strong)UILabel *couponDescLabel;
@property(nonatomic, strong)UILabel *couponLabel;
@property(nonatomic, strong)UIView *line;
@property(nonatomic, strong)UILabel *investDateDescLabel;
@property(nonatomic, strong)UILabel *investDateLabel;
@property(nonatomic, strong)UILabel *profitsDayDescLabel;
@property(nonatomic, strong)UILabel *profitsDayLabel;

@end


@implementation CYInvestRecordTopCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        self.topImageView = [[UIImageView alloc] initWithImage:[UIImage gradientImageFromColors:@[HEXCOLOR(@"3E8CE6"), HEXCOLOR(@"47BAE7")] ByGradientType:UIImageGradientTypeLeftToRight size:CGSizeMake(SCREEN_WIDTH, 125)]];
        [self.contentView addSubview:self.topImageView];
        
        self.statusLabel = [[UILabel alloc] init];
        self.statusLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
        self.statusLabel.textColor = HEXCOLOR(@"FFFFFF");
        [self.contentView addSubview:self.statusLabel];
        
        self.dueDayLabel = [[UILabel alloc] init];
        self.dueDayLabel.font = [UIFont systemFontOfSize:14];
        self.dueDayLabel.textColor = HEXCOLOR(@"FFFFFF");
        [self.contentView addSubview:self.dueDayLabel];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.textColor = HEXCOLOR(@"FFFFFF");
        [self.contentView addSubview:self.titleLabel];
        
        self.stateImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 86)];
        [self.contentView addSubview:self.stateImageView];
        
        self.amountDescLabel = [self labelWithText:@"投资金额(元)"];
        [self.contentView addSubview:self.amountDescLabel];
        
        self.amountLabel = [self labelWithText:nil];
        [self.contentView addSubview:self.amountLabel];
        
        self.rateDescLabel = [self labelWithText:@"年化收益率"];
        [self.contentView addSubview:self.rateDescLabel];
        
        self.rateLabel = [self labelWithText:nil];
        [self.contentView addSubview:self.rateLabel];
        
        self.profitsDescLabel = [self labelWithText:nil];
        [self.contentView addSubview:self.profitsDescLabel];
        
        self.profitsLabel = [self labelWithText:nil];
        [self.contentView addSubview:self.profitsLabel];
        
        self.couponDescLabel = [self labelWithText:@"使用优惠(元)"];
        [self.contentView addSubview:self.couponDescLabel];
        
        self.couponLabel = [self labelWithText:nil];
        [self.contentView addSubview:self.couponLabel];
        
        self.line = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.line];
        
        self.investDateDescLabel = [self labelWithText:@"投资时间"];
        [self.contentView addSubview:self.investDateDescLabel];
        
        self.investDateLabel = [self labelWithText:nil];
        [self.contentView addSubview:self.investDateLabel];
        
        self.profitsDayDescLabel = [self labelWithText:@"计息天数"];
        [self.contentView addSubview:self.profitsDayDescLabel];
        
        self.profitsDayLabel = [self labelWithText:nil];
        [self.contentView addSubview:self.profitsDayLabel];
    }
    return self;
}

+ (CGFloat)heightForObject:(CYInvestRecordTopCellObject *)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 335 + (object.model.coupon.doubleValue > 0 ? 34 : 0);
}

- (BOOL)shouldUpdateCellWithObject:(CYInvestRecordTopCellObject *)object
{
    self.titleLabel.text = object.model.title;
    self.dueDayLabel.hidden = object.model.progress != CYInvestProgressBearing;
    self.dueDayLabel.text = [NSString stringWithFormat:@"(剩余%@天)", object.model.dueDay.stringValue];
    
    self.statusLabel.text = @"";
    self.stateImageView.image = nil;
    
    if(object.model.progress <= CYInvestProgressAccount)
    {
        self.statusLabel.text = @"申购中";
        self.stateImageView.image = CYImage(@"invest_record_detail_account");
        self.profitsDescLabel.text = @"待收利息(元)";
    }
    else if(object.model.progress <= CYInvestProgressBearing)
    {
        self.statusLabel.text = @"计息中";
        self.stateImageView.image = CYImage(@"invest_record_detail_bearing");
        self.profitsDescLabel.text = @"待收利息(元)";
    }
    else
    {
        self.statusLabel.text = @"已还款";
        self.stateImageView.image = CYImage(@"invest_record_detail_in");
        self.profitsDescLabel.text = @"已收利息(元)";
    }
    
    self.amountLabel.text = object.model.amount.amountString;
    
    self.rateLabel.text = [NSString stringWithFormat:@"%.2f%%", object.model.rate.doubleValue];
    if(object.model.additionRate.doubleValue > 0)
    {
        self.rateLabel.text = [self.rateLabel.text stringByAppendingFormat:@"+%.2f%%", object.model.additionRate.doubleValue];
    }
    
    self.profitsLabel.text = object.model.profits.amountString;
    
    self.investDateLabel.text = object.model.investDate;
    
    self.profitsDayLabel.text = object.model.profitsDay.stringValue;
    
    if(object.model.coupon.doubleValue > 0)
    {
        self.couponLabel.text = object.model.coupon.amountString;
        self.couponDescLabel.hidden = NO;
        self.couponLabel.hidden = NO;
    }
    else
    {
        self.couponDescLabel.hidden = YES;
        self.couponLabel.hidden = YES;
    }
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.topImageView.left = 0;
    self.topImageView.top = 0;
    
    [self.statusLabel sizeToFit];
    self.statusLabel.left = 40;
    self.statusLabel.centerY = 49;
    
    [self.dueDayLabel sizeToFit];
    self.dueDayLabel.bottom = self.statusLabel.bottom;
    self.dueDayLabel.left = self.statusLabel.right;
    
    [self.titleLabel sizeToFit];
    self.titleLabel.left = 40;
    self.titleLabel.centerY = 77;
    
    self.stateImageView.right = self.contentView.width - 40;
    self.stateImageView.centerY = 125 / 2;
    
    CGFloat gap = 34;
    
    self.amountDescLabel.left = 20;
    self.amountDescLabel.centerY = self.topImageView.bottom + 27;
    [self.amountLabel sizeToFit];
    self.amountLabel.right = self.contentView.width - 20;
    self.amountLabel.centerY = self.amountDescLabel.centerY;
    
    self.rateDescLabel.left = 20;
    self.rateDescLabel.centerY = self.amountDescLabel.centerY + gap;
    [self.rateLabel sizeToFit];
    self.rateLabel.right = self.contentView.width - 20;
    self.rateLabel.centerY = self.rateDescLabel.centerY;
    
    [self.profitsDescLabel sizeToFit];
    self.profitsDescLabel.left = 20;
    self.profitsDescLabel.centerY = self.rateDescLabel.centerY + gap;
    [self.profitsLabel sizeToFit];
    self.profitsLabel.right = self.contentView.width - 20;
    self.profitsLabel.centerY = self.profitsDescLabel.centerY;
    
    if(self.couponLabel.hidden == NO)
    {
        self.couponDescLabel.left = 20;
        self.couponDescLabel.centerY = self.profitsDescLabel.centerY + gap;
        [self.couponLabel sizeToFit];
        self.couponLabel.right = self.contentView.width - 20;
        self.couponLabel.centerY = self.couponDescLabel.centerY;
    }
    
    self.line.width = self.contentView.width - 20;
    self.line.height = 1 / SCREEN_SCALE;;
    self.line.left = 20;
    self.line.top = self.contentView.height - 88;
    
    self.investDateDescLabel.left = 20;
    self.investDateDescLabel.centerY = self.line.top + 27;
    [self.investDateLabel sizeToFit];
    self.investDateLabel.right = self.contentView.width - 20;
    self.investDateLabel.centerY = self.investDateDescLabel.centerY;
    
    self.profitsDayDescLabel.left = 20;
    self.profitsDayDescLabel.centerY = self.investDateDescLabel.centerY + gap;
    [self.profitsDayLabel sizeToFit];
    self.profitsDayLabel.right = self.contentView.width - 20;
    self.profitsDayLabel.centerY = self.profitsDayDescLabel.centerY;
}

#pragma mark - Private Method
- (UILabel *)labelWithText:(NSString *)text
{
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = HEXCOLOR(@"666666");
    [label sizeToFit];
    return label;
}

@end
